package MineGod;

import java.util.ArrayList;

public class BlockAirGenerator extends BlockGenerator{
	
	
	public BlockAirGenerator(){
		super(BlockType.air);
		minVeinLength = 30;
		maxVeinLength = 40;
	}
	
	public Block provideBlock(){
		return new BlockAir();
	}

	public ArrayList<int[]> generateVein(Chunk chunk){
		int circleVeinDiam1 = Chunk.rand.nextInt(3)+9;
		int circleVeinDiam2 = Chunk.rand.nextInt(3)+8;
		
		int i = Chunk.rand.nextInt(chunk.blocks.length);
		int j = Chunk.rand.nextInt(chunk.blocks[i].length);
		int i2 = i + (Chunk.rand.nextInt(3)-1)*circleVeinDiam1/2;
		int j2 = j + (Chunk.rand.nextInt(3)-1)*circleVeinDiam1/2;
		
		
		chunk.blocks[i][j] = provideBlock();
		ArrayList<int[]> offRightOrLeft = new ArrayList<int[]>();
		ArrayList<int[]> valid = new ArrayList<int[]>();
		
		//loop through the square surrounding the initial vein starting position.
		for(int k = i - circleVeinDiam1/2; k < i + circleVeinDiam1/2; k++){
			for(int l = j - circleVeinDiam1/2; l < j + circleVeinDiam1/2; l++){
				//If square has a manhattan distance < radius it should be in the circle.
				if(Math.abs(i - k) + Math.abs(j - l) < circleVeinDiam1/2){
					valid.add(new int[] {k,l});
				}
				//else if square has euclidian distance < radius it should be in the circle.
				else if (Math.sqrt( (i-k)*(i-k)  + (j-l)*(j-l)) < circleVeinDiam1/2){
					valid.add(new int[] {k,l});
				}
			}
		}
		
		//loop through the square surrounding the initial vein starting position.
		for(int k = i2 - circleVeinDiam2/2; k < i2 + circleVeinDiam2/2; k++){
			for(int l = j2 - circleVeinDiam2/2; l < j2 + circleVeinDiam2/2; l++){
				//If square has a manhattan distance < radius it should be in the circle.
				if(Math.abs(i2 - k) + Math.abs(j2 - l) < circleVeinDiam2/2){
					valid.add(new int[] {k,l});
				}
				//else if square has euclidian distance < radius it should be in the circle.
				else if (Math.sqrt( (i2-k)*(i2-k)  + (j2-l)*(j2-l)) < circleVeinDiam2/2){
					valid.add(new int[] {k,l});
				}
			}
		}
		
		
		
		
		for(int[] array: valid){
			int m = array[0];
			int n = array[1];
			if(!(n < 0 || n >= Chunk.CHUNK_HEIGHT)){
				if(m >= chunk.blocks.length || m < 0) {
					offRightOrLeft.add(new int[] {m,n});
				}
				else{
					chunk.blocks[m][n] = provideBlock();
				}
			}
		}
		
		return offRightOrLeft;
	}
	
	public ArrayList<int[]> generateVeinOld(Chunk chunk) {
		int i = Chunk.rand.nextInt(chunk.blocks.length);
		int j = Chunk.rand.nextInt(chunk.blocks[i].length);
		
		chunk.blocks[i][j] = provideBlock();
		ArrayList<int[]> offRightOrLeft = new ArrayList<int[]>();
		
		//Now create the vein
		for (int k = 0; k < (Chunk.rand.nextInt(maxVeinLength-minVeinLength+1)+minVeinLength); k++){
			ArrayList<int[]> valid = new ArrayList<int[]>();
			for(int x = -1; x <= 1; x++) {
				for(int y = -1; y <= 1; y++) {
					if(!(x == 0 && y== 0)) {
						int checkX = i + x;
						int checkY = j + y;
						if(!(checkX < 0 || checkX >= chunk.blocks.length || checkY < 0 || checkY >= Chunk.CHUNK_HEIGHT) && !(chunk.blocks[checkX][checkY].type == type)){
							valid.add(new int[] {checkX,checkY});
						}
						else if ((checkX < 0 || checkX >= chunk.blocks.length) && !(checkY < 0 || checkY >= Chunk.CHUNK_HEIGHT)){
							valid.add(new int[] {checkX,checkY});
						}
					}
				}
			}
			
			if (valid.size() == 0){
				break;
			}
			int[] nextLoc = valid.get(Chunk.rand.nextInt(valid.size()));
			
			
			i = nextLoc[0];
			j = nextLoc[1];
			if(!(i < 0 || j < 0 || j >= Chunk.CHUNK_HEIGHT)){
				if(i >= chunk.blocks.length || i < 0) {
					offRightOrLeft.add(new int[] {i,j});
				}
				else{
					chunk.blocks[i][j] = provideBlock();
				}
			}
		}
		return offRightOrLeft;
	}
	
	
	
}
